//
//  SDVerCodeLoginViewController.m
//  skinDetection
//
//  Created by  liangzhun on 2018/10/29.
//  Copyright © 2018年  liangzhun. All rights reserved.
//

#import "SDVerCodeLoginViewController.h"
#import "SDVerificationCodeButton.h"
@interface SDVerCodeLoginViewController ()
@property (nonatomic ,strong)SDBoundNumberLinkView *linkView;
@property (nonatomic ,strong)SDVerificationCodeButton *getCodeBtn;
@end

@implementation SDVerCodeLoginViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.loginBtn.layer.cornerRadius = 5.f;
    self.loginBtn.layer.masksToBounds = YES;
    self.navHeight.constant = SDNavBar_Height;
    [self setTextField];
    
    [self.linkView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.view);
        make.bottom.equalTo(self.view).offset(-(20 + SDBottom_Height));
        make.height.mas_equalTo(15);
    }];
    
}
- (void)setTextField
{
    
    self.PhoneField.leftViewMode = UITextFieldViewModeAlways;
    
    self.VerCodeField.leftViewMode = UITextFieldViewModeAlways;
    self.VerCodeField.rightViewMode = UITextFieldViewModeAlways;
    self.PhoneField.rightViewMode = self.PhoneField.text.length > 0? UITextFieldViewModeAlways:UITextFieldViewModeNever;
    self.PhoneField.rightView = [self PhoneRightView];
    self.PhoneField.leftView = [self leftViewWithImageIcon:phoneicon];
    
    self.VerCodeField.leftView = [self leftViewWithImageIcon:codeicon];
    self.VerCodeField.rightView = [self codeVerRightView];
    
    
    [self.PhoneField addTarget:self action:@selector(textChange:) forControlEvents:UIControlEventEditingChanged];
    
    [self.VerCodeField addTarget:self action:@selector(textChange:) forControlEvents:UIControlEventEditingChanged];
    
}


- (UIView *)leftViewWithImageIcon:(NSString *)icon
{
    UIImage *imageicon = [UIImage imageNamed:icon];
    UIView *leftview =[[UIView alloc] initWithFrame:CGRectMake(0, 0, imageicon.size.width + 10, 54)];
    UIImageView *viewImage = [[UIImageView alloc] initWithImage:imageicon];
    [viewImage sizeThatFits:imageicon.size];
    viewImage.center = leftview.center;
    [leftview addSubview:viewImage];
    return leftview;
}
- (UIView *)PhoneRightView
{
    
    UIView *leftview =[[UIView alloc] initWithFrame:CGRectMake(0, 0, 50, 54)];
    
    UIButton *clearBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [clearBtn setImage:[UIImage imageNamed:clearicon] forState:UIControlStateNormal];
    [clearBtn handleControlEvent:UIControlEventTouchUpInside withBlock:^{
        self.PhoneField.text = nil;
        
        self.PhoneField.rightViewMode = UITextFieldViewModeNever;
    }];
    
    [clearBtn setFrame:CGRectMake(10, 0, 30, 54)];
    
    [leftview addSubview:clearBtn];
    return leftview;
    
}

- (void)textChange:(UITextField *)sender {
    
    if (sender == self.PhoneField) {
        
        if (sender.text.length >= 11){
            
            sender.text = [sender.text substringToIndex:11];
             self.getCodeBtn.phone = self.PhoneField.text;
            if (self.getCodeBtn.hasCode == YES)return;
            self.getCodeBtn.use = YES;
            
        }else{
            
           self.getCodeBtn.phone = self.PhoneField.text;
           if (self.getCodeBtn.hasCode == YES)return;
           self.getCodeBtn.use = NO;
        }
        
        
        sender.rightViewMode = sender.text.length > 0? UITextFieldViewModeAlways:UITextFieldViewModeNever;
    }
    
}
- (SDBoundNumberLinkView *)linkView
{
    if (!_linkView) {
        _linkView = [[SDBoundNumberLinkView alloc] init];
        [self.view addSubview:_linkView];
    }
    
    return _linkView;
    
}
-(SDVerificationCodeButton *)getCodeBtn
{
    if (!_getCodeBtn) {
        
        _getCodeBtn = [[SDVerificationCodeButton alloc] init];
        @weakify(self);
    
        [_getCodeBtn handleControlEvent:UIControlEventTouchUpInside withBlock:^{
            @strongify(self);
            if ([SDMethodTool validateCellPhoneNumber:self.PhoneField.text]) {
                
                self.getCodeBtn.hasCode = YES;
                self.getCodeBtn.use = NO;
                [self.VerCodeField becomeFirstResponder];
                [self getVerificationCode];
                
            }else{
              
                [SDMethodTool alertWithContent:GetCodeWarn handler:nil];
                
            }
            
           
        }];
       
    }
    
    return _getCodeBtn;
    
}

/**
 获取验证码
 */
- (void)getVerificationCode
{
    
    NSDictionary *parameter = @{@"userName":self.PhoneField.text,@"mark":@1};
    
    [SDNetwork getVerCodeWithParameters:parameter finish:^(NSDictionary * _Nullable dic, NSError * _Nullable error) {
         if (!error) {
             [SDMethodTool showWarnWithContent:GetCodeSuccess];
         }
    }];
    
}
- (UIView *)codeVerRightView
{

    UIView *leftview =[[UIView alloc] initWithFrame:CGRectMake(0, 0, 85, 54)];

    [self.getCodeBtn setFrame:CGRectMake(0, 0, 85, 25)];
    self.getCodeBtn.center = CGPointMake(85/2, 55/2.0);
    [leftview addSubview:self.getCodeBtn];

    return leftview;

}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/



- (IBAction)back:(UIButton *)sender {
    [self.navigationController popViewControllerAnimated:YES];
}

- (IBAction)login:(UIButton *)sender {
    
     self.linkView.selectBtn.selected == NO?[self alertWithContent:BoundWarnProtocol]:self.PhoneField.text.length == 0? [self alertWithContent:BoundWarn1]:self.VerCodeField.text.length == 0?[self alertWithContent:BoundWarn2]:[self loginVerCode];
    
}
- (void)alertWithContent:(NSString *)content
{
    [SDMethodTool alertWithContent:content handler:nil];
}

/**
 验证码登录
 */
- (void)loginVerCode
{
    
    NSDictionary *dic = @{@"userName":self.PhoneField.text,@"code":self.VerCodeField.text,@"mark":@1,@"device":[SDMethodTool iphoneType],@"appVer":APP_Version,@"imei":APP_DEVICE_UNIQUE};
    
    [SDNetwork postSMSLoginWithParameters:dic finish:^(NSDictionary * _Nullable dic, NSError * _Nullable error) {
        if (!error) {
            
            USERDEFAULT_REMOVE(KEY_USERMSG);
            USERDEFAULT_PUT(dic[@"data"], KEY_USERMSG);
            NSTimeInterval timeInt = [[NSDate date] timeIntervalSince1970];
            USERDEFAULT_PUT([NSNumber numberWithDouble:timeInt], KEY_USERMSG_SAVETIME);
            SDAppDelegate.userModel = [SDUserInformationModel modelObjectWithDictionary:dic[@"data"]];
            [SDAppDelegate goTabbar];
            
        }
    }];
}
@end
